package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"strconv"
	"strings"
	"zhice/models"
)

type OrderController struct {
	beego.Controller
}

type JsonData struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *OrderController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "文件大小必须大于2MB",
			Data: nil,
		}
		c.ServeJSON()
	}
	suffix := strings.ToLower(filepath.Ext(header.Filename))
	suffixMax := map[string]bool{
		".png": true,
		".jpg": true,
	}
	if !suffixMax[suffix] {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "文件类型只能是.png,.jpg",
			Data: nil,
		}
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		ImgSrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonData{
			Code: 200,
			Msg:  "文件上传成功",
			Data: ImgSrc,
		}
		c.ServeJSON()
	}
}
func (c *OrderController) List() {
	page, _ := c.GetInt("p", 1)
	pageSize, _ := c.GetInt("s", 3)
	name := c.GetString("name")
	key := "page-" + strconv.Itoa(page) + "-" + strconv.Itoa(pageSize) + "-" + name
	val, err := models.Rdb.Get(key).Result()
	JsonList := make(map[string]interface{})
	if err != nil {
		orders, total, err := models.MysqlList(page, pageSize, name)
		if err != nil {
			c.Data["json"] = JsonData{
				Code: 500,
				Msg:  "分页失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {

			JsonList["orders"] = orders
			JsonList["total"] = total
			jsonByte, _ := json.Marshal(JsonList)
			models.Rdb.Set(key, string(jsonByte), 0)
			c.Data["json"] = JsonData{
				Code: 200,
				Msg:  "分页成功",
				Data: JsonList,
			}
			c.ServeJSON()
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &JsonList)
		c.Data["json"] = JsonData{
			Code: 200,
			Msg:  "分页成功-redis",
			Data: JsonList,
		}
		c.ServeJSON()
	}

}
func (c *OrderController) Rank() {
	result, err := models.Rdb.ZRevRange("Order-rank", 0, 9).Result()
	if err != nil {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "排行失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	var rankSlice []models.Order
	for _, number := range result {
		var order models.Order
		models.DB.Model(&models.Order{}).Where("id = ?", number).First(&order)
		rankSlice = append(rankSlice, order)
	}
	c.Data["json"] = JsonData{
		Code: 200,
		Msg:  "排行成功",
		Data: rankSlice,
	}
	c.ServeJSON()
}
func (c *OrderController) Rank2() {
	reurlt, err := models.Rdb.ZRevRange("order-rank", 0, 9).Result()
	if err != nil {
		c.Data["json"] = JsonData{
			Code: 500,
			Msg:  "排行失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	var rankSlice []models.Order
	for _, number := range reurlt {
		var order models.Order
		models.DB.Model(&order).Where("id = ?", number).First(&order)
		rankSlice = append(rankSlice, order)
	}
	c.Data["json"] = JsonData{
		Code: 200,
		Msg:  "排行成功",
		Data: nil,
	}
}
func (c *OrderController) Show() {
	var order models.Order
	id, _ := c.GetInt("id")
	OrderRank := "order-bank"
	number := strconv.Itoa(id)
	models.Rdb.ZIncrBy(OrderRank, 1, number)
	key := "order-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err := models.DB.Model(&order).Where("id = ?", id).First(&order).Error
		if err != nil {
			c.Data["json"] = JsonData{
				Code: 500,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			KeyByte, _ := json.Marshal(order)
			models.Rdb.Set(key, string(KeyByte), 0)
			c.Data["json"] = JsonData{
				Code: 200,
				Msg:  "展示成功",
				Data: order,
			}
			c.ServeJSON()
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &order)
		c.Data["json"] = JsonData{
			Code: 200,
			Msg:  "展示成功-redis",
			Data: order,
		}
	}

}
func (c *OrderController) SHow() {
	var order []models.Order
	id, _ := c.GetInt("id")
	orderbank := "order-bank"
	number := strconv.Itoa(id)
	models.Rdb.ZIncrBy(orderbank, 1, number)
	key := "rank" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err := models.DB.Model(&order).Where("id = ?", id).First(&order)
		if err != nil {
			c.Data["json"] = JsonData{
				Code: 200,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			jsonbyte, _ := json.Marshal(order)
			models.Rdb.Set(key, string(jsonbyte), 0)
			c.Data["josn"] = JsonData{
				Code: 200,
				Msg:  "展示成功",
				Data: order,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), key)
		c.Data["json"] = JsonData{
			Code: 200,
			Msg:  "展示成功-redis",
			Data: order,
		}
		c.ServeJSON()
	}

}
